import {
    Controller,
    Get,
    Post,
    Body,
    Put,
    Param,
    Query,
    DefaultValuePipe,
    Delete,
    HttpStatus,
} from '@nestjs/common';
import {
    ApiTags,
    ApiOperation,
    ApiBearerAuth,
    ApiResponse,
    ApiQuery,
    ApiParam,
} from '@nestjs/swagger';
import {MeetingService} from './meeting.service';
import {CreateMeetingDto} from './dto/create-meeting.dto';
import {UpdateMeetingDto} from './dto/update-meeting.dto';
import {
    RequireLogin,
    RequirePermission,
} from 'src/middleware/RBAC/custom.decorator';
import {PublicVoSuccess, PublicVoError} from '../utils/publicVo/index';
import {generateParseIntPipe} from '../utils/paging/index';
import {MeetingListVo} from './vo/list.vo';
import {MeetingDetailsVo} from './vo/details.vo';

@ApiTags('会议室管理模块')
@Controller('meeting')
export class MeetingController {
    constructor(private readonly meetingService: MeetingService) {
    }

    // 插入初始化数据
    @Get('init-data')
    @ApiOperation({summary: '初始化数据', description: '插入初始化数据接口'})
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: PublicVoSuccess,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    async initData() {
        await this.meetingService.initData();
        return 'done';
    }

    @Get()
    @ApiOperation({summary: '会议室列表', description: '会议室列表接口'})
    @ApiQuery({
        name: 'pageNum',
        type: String,
        description: '页码',
        required: true,
    })
    @ApiQuery({
        name: 'pageSize',
        type: String,
        description: '页数',
        required: true,
    })
    @ApiQuery({
        name: 'name',
        type: String,
        description: '会议室名称',
        required: false,
    })
    @ApiQuery({
        name: 'capacity',
        type: String,
        description: '会议室容量',
        required: false,
    })
    @ApiQuery({
        name: 'equipment',
        type: String,
        description: '会议室设备',
        required: false,
    })
    @ApiBearerAuth()
    @RequireLogin()
    @RequirePermission()
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: MeetingListVo,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    async list(
        @Query('pageNum', new DefaultValuePipe(1), generateParseIntPipe('pageNo'))
            pageNum: number,
        @Query(
            'pageSize',
            new DefaultValuePipe(2),
            generateParseIntPipe('pageSize'),
        )
            pageSize: number,
        @Query('name') name: string,
        @Query('capacity') capacity: number,
        @Query('equipment') equipment: string,
    ) {
        return await this.meetingService.findAll(
            pageNum,
            pageSize,
            name,
            capacity,
            equipment,
        );
    }

    @Get(':id')
    @ApiOperation({summary: '会议室详情', description: '会议室详情接口'})
    @ApiParam({
        name: 'id',
        type: String,
        description: '会议室id',
        required: true,
    })
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: MeetingDetailsVo,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    @ApiBearerAuth()
    @RequireLogin()
    @RequirePermission()
    findOne(@Param('id') id: string) {
        return this.meetingService.findOne(Number(id));
    }

    @Delete(':id')
    @ApiOperation({summary: '会议室删除', description: '会议室删除接口'})
    @ApiParam({
        name: 'id',
        type: String,
        description: '会议室id',
        required: true,
    })
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: PublicVoSuccess,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    @ApiBearerAuth()
    @RequireLogin()
    @RequirePermission()
    remove(@Param('id') id: string) {
        return this.meetingService.remove(+id);
    }

    @Put()
    @ApiOperation({summary: '会议室修改', description: '会议室修改接口'})
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: PublicVoSuccess,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    @ApiBearerAuth()
    @RequireLogin()
    @RequirePermission()
    update(@Body() updateMeetingDto: UpdateMeetingDto) {
        return this.meetingService.update(updateMeetingDto);
    }

    @Post()
    @ApiOperation({summary: '会议室新增', description: '会议室新增接口'})
    @ApiBearerAuth()
    @RequireLogin()
    @RequirePermission()
    @ApiResponse({
        status: HttpStatus.OK,
        description: '请求成功',
        type: PublicVoSuccess,
    })
    @ApiResponse({
        status: HttpStatus.BAD_REQUEST,
        description: '请求失败',
        type: PublicVoError,
    })
    create(@Body() createMeetingDto: CreateMeetingDto) {
        return this.meetingService.create(createMeetingDto);
    }
}
